﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using Fjaspx.DB;
using System.Collections.Generic;
namespace Fjaspx.Sys.Dal
{
    /// <summary>
    /// ���ݷ�����Fjaspx_Photos��
    /// </summary>
    public class Fjaspx_Photos
    {
        public Fjaspx_Photos()
        { }
        #region  ��Ա����

        /// <summary>
        /// �õ����ID
        /// </summary>
        public int GetMaxId()
        {
            return DbHelperSQL.GetMaxID("PhotoId", "Fjaspx_Photos");
        }

        /// <summary>
        /// �Ƿ���ڸü�¼
        /// </summary>
        public bool Exists(int PhotoId)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from Fjaspx_Photos");
            strSql.Append(" where PhotoId=@PhotoId ");
            SqlParameter[] parameters = {
					new SqlParameter("@PhotoId", SqlDbType.Int,4)};
            parameters[0].Value = PhotoId;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// ����һ������
        /// </summary>
        public int Add(Model.Fjaspx_Photos model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into Fjaspx_Photos(");
            strSql.Append("FileName,Attachment,FileSize,Title,Description,Postdate,Albumid,UserId,UserName,Views,Comments,Isattachment,Width,Height)");
            strSql.Append(" values (");
            strSql.Append("@FileName,@Attachment,@FileSize,@Title,@Description,@Postdate,@Albumid,@UserId,@UserName,@Views,@Comments,@Isattachment,@Width,@Height)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@FileName", SqlDbType.Char,255),
					new SqlParameter("@Attachment", SqlDbType.NChar),
					new SqlParameter("@FileSize", SqlDbType.Int,4),
					new SqlParameter("@Title", SqlDbType.NChar),
					new SqlParameter("@Description", SqlDbType.NChar),
					new SqlParameter("@Postdate", SqlDbType.DateTime),
					new SqlParameter("@Albumid", SqlDbType.Int,4),
					new SqlParameter("@UserId", SqlDbType.Int,4),
					new SqlParameter("@UserName", SqlDbType.NChar),
					new SqlParameter("@Views", SqlDbType.Int,4),
					new SqlParameter("@Comments", SqlDbType.Int,4),
					new SqlParameter("@Isattachment", SqlDbType.Int,4),
					new SqlParameter("@Width", SqlDbType.Int,4),
					new SqlParameter("@Height", SqlDbType.Int,4)};
            parameters[0].Value = model.FileName;
            parameters[1].Value = model.Attachment;
            parameters[2].Value = model.FileSize;
            parameters[3].Value = model.Title;
            parameters[4].Value = model.Description;
            parameters[5].Value = model.Postdate;
            parameters[6].Value = model.Albumid;
            parameters[7].Value = model.UserId;
            parameters[8].Value = model.UserName;
            parameters[9].Value = model.Views;
            parameters[10].Value = model.Comments;
            parameters[11].Value = model.Isattachment;
            parameters[12].Value = model.Width;
            parameters[13].Value = model.Height;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 1;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// ����һ������
        /// </summary>
        public void Update(Model.Fjaspx_Photos model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update Fjaspx_Photos set ");
            strSql.Append("FileName=@FileName,");
            strSql.Append("Attachment=@Attachment,");
            strSql.Append("FileSize=@FileSize,");
            strSql.Append("Title=@Title,");
            strSql.Append("Description=@Description,");
            strSql.Append("Postdate=@Postdate,");
            strSql.Append("Albumid=@Albumid,");
            strSql.Append("UserId=@UserId,");
            strSql.Append("UserName=@UserName,");
            strSql.Append("Views=@Views,");
            strSql.Append("Comments=@Comments,");
            strSql.Append("Isattachment=@Isattachment,");
            strSql.Append("Width=@Width,");
            strSql.Append("Height=@Height");
            strSql.Append(" where PhotoId=@PhotoId ");
            SqlParameter[] parameters = {
					new SqlParameter("@PhotoId", SqlDbType.Int,4),
					new SqlParameter("@FileName", SqlDbType.Char,255),
					new SqlParameter("@Attachment", SqlDbType.NChar),
					new SqlParameter("@FileSize", SqlDbType.Int,4),
					new SqlParameter("@Title", SqlDbType.NChar),
					new SqlParameter("@Description", SqlDbType.NChar),
					new SqlParameter("@Postdate", SqlDbType.DateTime),
					new SqlParameter("@Albumid", SqlDbType.Int,4),
					new SqlParameter("@UserId", SqlDbType.Int,4),
					new SqlParameter("@UserName", SqlDbType.NChar),
					new SqlParameter("@Views", SqlDbType.Int,4),
					new SqlParameter("@Comments", SqlDbType.Int,4),
					new SqlParameter("@Isattachment", SqlDbType.Int,4),
					new SqlParameter("@Width", SqlDbType.Int,4),
					new SqlParameter("@Height", SqlDbType.Int,4)};
            parameters[0].Value = model.PhotoId;
            parameters[1].Value = model.FileName;
            parameters[2].Value = model.Attachment;
            parameters[3].Value = model.FileSize;
            parameters[4].Value = model.Title;
            parameters[5].Value = model.Description;
            parameters[6].Value = model.Postdate;
            parameters[7].Value = model.Albumid;
            parameters[8].Value = model.UserId;
            parameters[9].Value = model.UserName;
            parameters[10].Value = model.Views;
            parameters[11].Value = model.Comments;
            parameters[12].Value = model.Isattachment;
            parameters[13].Value = model.Width;
            parameters[14].Value = model.Height;

            DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
        }

        /// <summary>
        /// ɾ��һ������
        /// </summary>
        public void Delete(int PhotoId)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete Fjaspx_Photos ");
            strSql.Append(" where PhotoId=@PhotoId ");
            SqlParameter[] parameters = {
					new SqlParameter("@PhotoId", SqlDbType.Int,4)};
            parameters[0].Value = PhotoId;

            DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
        }


        /// <summary>
        /// �õ�һ������ʵ��
        /// </summary>
        public Model.Fjaspx_Photos GetModel(int PhotoId)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select PhotoId,FileName,Attachment,FileSize,Title,Description,Postdate,Albumid,UserId,UserName,Views,Comments,Isattachment,Width,Height from Fjaspx_Photos ");
            strSql.Append(" where PhotoId=@PhotoId ");
            SqlParameter[] parameters = {
					new SqlParameter("@PhotoId", SqlDbType.Int,4)};
            parameters[0].Value = PhotoId;

            Model.Fjaspx_Photos model = new Model.Fjaspx_Photos();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["PhotoId"].ToString() != "")
                {
                    model.PhotoId = int.Parse(ds.Tables[0].Rows[0]["PhotoId"].ToString());
                }
                model.FileName = ds.Tables[0].Rows[0]["FileName"].ToString();
                model.Attachment = ds.Tables[0].Rows[0]["Attachment"].ToString();
                if (ds.Tables[0].Rows[0]["FileSize"].ToString() != "")
                {
                    model.FileSize = int.Parse(ds.Tables[0].Rows[0]["FileSize"].ToString());
                }
                model.Title = ds.Tables[0].Rows[0]["Title"].ToString();
                model.Description = ds.Tables[0].Rows[0]["Description"].ToString();
                if (ds.Tables[0].Rows[0]["Postdate"].ToString() != "")
                {
                    model.Postdate = DateTime.Parse(ds.Tables[0].Rows[0]["Postdate"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Albumid"].ToString() != "")
                {
                    model.Albumid = int.Parse(ds.Tables[0].Rows[0]["Albumid"].ToString());
                }
                if (ds.Tables[0].Rows[0]["UserId"].ToString() != "")
                {
                    model.UserId = int.Parse(ds.Tables[0].Rows[0]["UserId"].ToString());
                }
                model.UserName = ds.Tables[0].Rows[0]["UserName"].ToString();
                if (ds.Tables[0].Rows[0]["Views"].ToString() != "")
                {
                    model.Views = int.Parse(ds.Tables[0].Rows[0]["Views"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Comments"].ToString() != "")
                {
                    model.Comments = int.Parse(ds.Tables[0].Rows[0]["Comments"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Isattachment"].ToString() != "")
                {
                    model.Isattachment = int.Parse(ds.Tables[0].Rows[0]["Isattachment"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Width"].ToString() != "")
                {
                    model.Width = int.Parse(ds.Tables[0].Rows[0]["Width"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Height"].ToString() != "")
                {
                    model.Height = int.Parse(ds.Tables[0].Rows[0]["Height"].ToString());
                }
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// ��������б�
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select PhotoId,FileName,Attachment,FileSize,Title,Description,Postdate,Albumid,UserId,UserName,Views,Comments,Isattachment,Width,Height ");
            strSql.Append(" FROM Fjaspx_Photos ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }
        /// <summary>
        /// ��������б�
        /// </summary>
        public List<Model.Fjaspx_Photos> GetList(int page, int len, ref int pages)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select PhotoId,FileName,Attachment,FileSize,Title,Description,Postdate,Albumid,UserId,UserName,Views,Comments,Isattachment,Width,Height ");
            strSql.Append(" FROM Fjaspx_Photos ");
            return LoadListFromDataTable(DbHelperSQL.Query(strSql.ToString()).Tables[0].DefaultView);
        }
        /// <summary>
        /// ��������б�
        /// </summary>
        public List<Model.Fjaspx_Photos> GetList()
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select PhotoId,FileName,Attachment,FileSize,Title,Description,Postdate,Albumid,UserId,UserName,Views,Comments,Isattachment,Width,Height ");
            strSql.Append(" FROM Fjaspx_Photos ");
            //if (strWhere.Trim() != "")
            //{
            //    strSql.Append(" where " + strWhere);
            //}
            return LoadListFromDataTable(DbHelperSQL.Query(strSql.ToString()).Tables[0].DefaultView);
        }
        private List<Model.Fjaspx_Photos> LoadListFromDataTable(DataView dv)
        {
            List<Model.Fjaspx_Photos> list = new List<Model.Fjaspx_Photos>();
            for (int index = 0; index <= (dv.Count - 1); index++)
            {
                list.Add(new Model.Fjaspx_Photos(dv[index].Row));
            }
            return list;
        }

        #endregion
    }
}

